package com.sztx.pay.center.rpc.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sztx.pay.center.common.annotation.SlaveQueryAnnotation;
import com.sztx.pay.center.common.util.Constance;
import com.sztx.pay.center.common.util.Constance.TradeOrderType;
import com.sztx.pay.center.common.util.Constance.UserActType;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.core.service.ActService;
import com.sztx.pay.center.core.service.BillnoService;
import com.sztx.pay.center.core.service.UserInfoService;
import com.sztx.pay.center.dataaccess.dao.mysql.PlatActFlowMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.PlatActFlowDO;
import com.sztx.pay.center.dataaccess.domain.UserActDO;
import com.sztx.pay.center.dataaccess.domain.UserInfoDO;
import com.sztx.pay.center.rpc.api.domain.PageModelResponse;
import com.sztx.pay.center.rpc.api.domain.PlusMoneyRequest;
import com.sztx.pay.center.rpc.api.domain.PlusMoneyResponse;
import com.sztx.pay.center.rpc.api.service.PlatActPlusMoneyRpcService;
import com.sztx.se.common.exception.BusinessException;
import com.sztx.se.common.util.StringUtil;
import com.sztx.se.dataaccess.mysql.config.PageQuery;
@Service
public class PlatActPlusMoneyRpcServiceImpl implements PlatActPlusMoneyRpcService{
	@Autowired
	PlatActFlowMysqlDAO platActFlowMysqlDAO;
	@Autowired
	UserActMysqlDAO userActMysqlDAO;
	@Autowired
	ActService actService;
	@Autowired
	UserInfoService userInfoService;
	@Autowired
	BillnoService billnoService;
	
	@Override
	@Transactional
	public Integer addMoney(PlusMoneyRequest request) {
		Integer actType = request.getActType();
		Long amount = request.getAmount();
		String remark = request.getRemark();
		String user = request.getUser();
		String userId = request.getUserId();
		int plusType = request.getPlusType();
		ValidateUtil.isNotBlank(user, "操作人不能为空");
		ValidateUtil.isNotBlank(userId, "帐号不能为空");
		ValidateUtil.gt(amount, new Long(0), "加款金额不能小于0");
//		if(plusType == 1){
//			ValidateUtil.gt(amount, new Long(0), "加款金额不能小于0");
//		}else if(plusType == 2){
//			ValidateUtil.lt(amount.intValue(), 0, "加款金额不能大于0");
//		}
		ValidateUtil.isBetween(plusType,1, 2, "plusType只能为1或者2");
		ValidateUtil.notNull(actType, "账户类型不能为空");
		if(StringUtil.isNotBlank(remark)){
			ValidateUtil.strMaxLen(remark, 255, "remark长度超过限制");
		}
		if(actType != UserActType.CASH.getCode()){
			throw new BusinessException("非余额账户,不允许加款");
		}
		
		
		UserInfoDO userInfo = userInfoService.findUserInfo(userId);
		if(userInfo == null){
			throw new BusinessException("未查询到帐号【"+userId+"】");
		}
		boolean isSZTXUser = (userInfo.getUserType().intValue() & Constance.UserType.SZTX.getCode()) != 0;
		
		if(isSZTXUser == false){
			throw new BusinessException("该帐号不为平台帐号,不允许加款");
		}
		UserActType userActType = UserActType.valueOf(actType);
		if(userActType == null){
			throw new BusinessException("账户类型错误,不允许加款");
		}
		UserActDO userActDO = actService.findUserAct(userId, userActType);
		if(userActDO == null){
			throw new BusinessException("未查询到帐号【"+userId+"】的账户类型【"+actType+"】,不允许加款");
		}
		String platActFlowNo = billnoService.nextNo(1027);
		
		PlatActFlowDO platActFlowDO = new PlatActFlowDO();
		platActFlowDO.setActNo(userActDO.getActNo());
		platActFlowDO.setActType(actType);
		if(plusType == 2){
			platActFlowDO.setAmount(-amount);
		}else{
			platActFlowDO.setAmount(amount);
		}
		
		platActFlowDO.setCreateUser(user);
		platActFlowDO.setPlatActFlowNo(platActFlowNo);
		platActFlowDO.setRemark(remark);
		platActFlowDO.setUpdateUser(user);
		platActFlowDO.setUserId(userId);
		platActFlowDO.setActRemark(userInfo.getRemark());
		
		if(plusType == 1){
			actService.plusMoney(userActDO.getActNo(), amount, platActFlowNo, platActFlowNo, new Date(), TradeOrderType.PLUS_ADD_MONEY, "中间账户加款", "0009");
		}else if(plusType == 2){
			actService.subMoney(userActDO.getActNo(), amount, platActFlowNo, platActFlowNo, new Date(), TradeOrderType.PLUS_ADD_MONEY, "中间账户减款", "0009");
		}
		
		
		
		Integer saveFlag = platActFlowMysqlDAO.save(platActFlowDO);//写流水
		
		//userActMysqlDAO.addMoney(userActDO.getActNo(), amount, user);//加款
		return saveFlag;
	}

	@Override
	@SlaveQueryAnnotation
	public PageModelResponse<PlusMoneyResponse> findByPage(String userId,
			Integer actType, int pageNo, int pageSize) {
		PlatActFlowDO platActFlowDO = new PlatActFlowDO();
		platActFlowDO.setUserId(userId);
		platActFlowDO.setActType(actType);
		PageQuery pageQuery = new PageQuery(pageNo, pageSize);
		Integer listCount = platActFlowMysqlDAO.listCount(platActFlowDO );
		PageModelResponse<PlusMoneyResponse> response = new PageModelResponse<PlusMoneyResponse>();
		List<PlusMoneyResponse> list = new ArrayList<PlusMoneyResponse>();
		if(listCount > 0){
			List<PlatActFlowDO> platActFlowDOList = platActFlowMysqlDAO.listPage(platActFlowDO, pageQuery);
			for (PlatActFlowDO platActFlowDO2 : platActFlowDOList) {
				PlusMoneyResponse tmp = new PlusMoneyResponse();
				tmp.setActNo(platActFlowDO2.getActNo());
				tmp.setActType(platActFlowDO2.getActType());
				tmp.setAmount(platActFlowDO2.getAmount());
				tmp.setCreateTime(platActFlowDO2.getCreateTime());
				tmp.setCreateUser(platActFlowDO2.getCreateUser());
				tmp.setModifyTime(platActFlowDO2.getModifyTime());
				tmp.setPlatActFlowNo(platActFlowDO2.getPlatActFlowNo());
				tmp.setRemark(platActFlowDO2.getRemark());
				tmp.setUpdateUser(platActFlowDO2.getUpdateUser());
				tmp.setUserId(platActFlowDO2.getUserId());
				tmp.setActRemark(platActFlowDO2.getActRemark());
				tmp.setPreActBalance(platActFlowDO2.getPreActBalance());
				list.add(tmp);
			}
		}
		
		response.setList(list);
		response.setPageNo(pageNo);
		response.setPageSize(pageSize);
		response.setTotal(listCount);
		return response;
	}

}
